package what.basic.table;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import what.basic.basiccomponent.*;
public class Jtablewithcheckbox {
	Jtablewithcheckbox(){
		JFrame f=new Jframe().jframe("",1000,100,true);
		final Mytable3 my=new Mytable3();
		final JTable table=new JTable(my);
		JCheckBox c=new JCheckBox();
		
		table.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(c));
		table.setPreferredScrollableViewportSize(new Dimension(550,30));
		JScrollPane sp=new JScrollPane(table);
		f.getContentPane().add(sp,BorderLayout.CENTER);
		my.addTableModelListener(new TableModelListener(){
			public void tableChanged(TableModelEvent e) {
				int row=e.getFirstRow();
				int grade1=Integer.parseInt(my.getValueAt(row,1).toString());
				int grade2=Integer.parseInt(my.getValueAt(row,2).toString());
				int total=grade1+grade2;
				my.mySetValueAt(new Integer(total), row, 3);
				table.repaint();
			}
		});
	}
	public static void main(String[] args) {
		new Jtablewithcheckbox();

	}
}
class Mytable3 extends AbstractTableModel{
	Object[][] st={
			{"wangpeng",new Integer(91),new Integer(100),new Integer(100)}
			,{"zhuxuelian",new Integer(82),new Integer(69),new Integer(100)},
			{"meiting",new Integer(47),new Integer(57),new Integer(100)},
	};
	String[] names={"name","yuwen","shuxue","zongfen"};
	@Override
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return names.length;
	}
	@Override
	public int getRowCount() {
		// TODO Auto-generated method stub
		return st.length;
	}
	@Override
	public Object getValueAt(int row, int col) {
		// TODO Auto-generated method stub
		return st[row][col];
	}
	public String getColumnName(int col){
		return names[col];
	}
	public Class getColumnClass(int c){
		return getValueAt(0,c).getClass();
	}
	public boolean isCellEditable(int rowIndex,int columnIndex){
		return true;
	}
	public void setValueAt(Object value,int row,int col){
		st[row][col]=value;
		fireTableCellUpdated(row,col);
	}
	public void mySetValueAt(Object value,int row,int col){
		st[row][col]=value;
	}
}
